
#import "SMMineOrdersViewController.h"
#import <Masonry/Masonry.h>
#import "SMListCardCell.h"

@interface SMMineOrdersViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UISegmentedControl *seg;
@property(nonatomic,strong) UITableView *table;
@property(nonatomic,strong) NSArray<NSDictionary*> *data;
@end

@implementation SMMineOrdersViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的订单";
    self.view.backgroundColor=UIColor.systemGroupedBackgroundColor;
    _seg=[[UISegmentedControl alloc] initWithItems:@[@"全部",@"待处理",@"已完成"]];
    _seg.selectedSegmentIndex=0;
    [_seg addTarget:self action:@selector(reload) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_seg];
    [_seg mas_makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(8); make.left.equalTo(self.view).offset(12); make.right.equalTo(self.view).offset(-12);}];
    _table=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _table.backgroundColor=UIColor.systemGroupedBackgroundColor; _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_table registerClass:SMListCardCell.class forCellReuseIdentifier:@"cell"];
    _table.dataSource=self; _table.delegate=self;
    [self.view addSubview:_table];
    [_table mas_makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(self->_seg.mas_bottom).offset(8); make.left.right.bottom.equalTo(self.view);}];
    [self reload];
}
- (void)reload {
    NSMutableArray *arr=[NSMutableArray array];
    for(int i=0;i<12;i++){
        NSString *img=[NSString stringWithFormat:@"https://picsum.photos/seed/order_%ld_%d/520/360",(long)_seg.selectedSegmentIndex,i];
        NSString *title=[NSString stringWithFormat:@"订单 #%d", i+1001];
        NSString *sub= (_seg.selectedSegmentIndex==1)?@"待支付 · 等待确认":((_seg.selectedSegmentIndex==2)?@"已完成 · 感谢您的合作":@"交易进行中 · 已发货");
        NSString *meta=@"金额 ¥2,450";
        [arr addObject:@{@"img":img,@"title":title,@"sub":sub,@"meta":meta}];
    }
    self.data=arr; [self.table reloadData];
}
- (NSInteger)tableView:(UITableView*)t numberOfRowsInSection:(NSInteger)s{ return self.data.count; }
- (CGFloat)tableView:(UITableView*)t heightForRowAtIndexPath:(NSIndexPath*)i{ return 110; }
- (UITableViewCell*)tableView:(UITableView*)t cellForRowAtIndexPath:(NSIndexPath*)i{
    SMListCardCell *c=[t dequeueReusableCellWithIdentifier:@"cell" forIndexPath:i];
    NSDictionary *d=self.data[i.row];
    [c configureWithImageURL:d[@"img"] title:d[@"title"] subtitle:d[@"sub"] meta:d[@"meta"]];
    return c;
}
@end
